﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DirectShowLib;
using System.Runtime.InteropServices;

namespace Sample
{
    public partial class Form1 : Form
    {
        // DShow용 그래프와 컨트롤 선언
        IGraphBuilder pGraphBuilder = null;
        IMediaControl pMediaControl = null;

        // 이벤트 사용
        IMediaEvent pMediaEvent = null;
        EventCode eventCode;
        
        // 윈도우 제목
        IVideoWindow pVideoWindow = null;
        
        // WinForm
        System.Windows.Forms.Panel panel;

        public Form1()
        {
            InitializeComponent();

            panel = new Panel();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 새 필터 그래프 대응
            pGraphBuilder = (IGraphBuilder)new FilterGraph();
            // 필터 그래프에 컨트롤 붙이기
            pMediaControl = (IMediaControl)pGraphBuilder;
            // 필터 그래프에 이벤트 붙이기
            pMediaEvent = (IMediaEvent)pGraphBuilder;

            // 동영상 불러오기
            pMediaControl.RenderFile("E:\\동영상\\미드\\빅뱅이론\\Season 2\\The.Big.Bang.Theory.S02E03.HDTV.XviD-LOL.avi");

            pVideoWindow = (IVideoWindow)pGraphBuilder;
            // 플레이 window 생성해서 재생
            /*            
            pVideoWindow.put_FullScreenMode(OABool.True);
            pVideoWindow.put_Caption("연습");
             */

            // panel에서 재생
            pVideoWindow.put_Owner(panel1.Handle);
            pVideoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipSiblings);
            Rectangle rect = panel1.ClientRectangle;
            pVideoWindow.SetWindowPosition(0, 0, rect.Right, rect.Bottom);

            // 재생!
            pMediaControl.Run();

            pMediaEvent.WaitForCompletion(3000, out eventCode);

            switch (eventCode)
            {
                case 0:
                    textBox1.Text = "Timeout!";
                    break;
                case EventCode.UserAbort:
                    textBox1.Text = "User Abort";
                    break;

                case EventCode.Complete:
                    textBox1.Text = "Completed.";
                    break;

                case EventCode.ErrorAbort:
                    textBox1.Text = "Error Abort";
                    break;
            }

            // 그래프와 컨트롤 해제
            Marshal.ReleaseComObject(pGraphBuilder);
            Marshal.ReleaseComObject(pMediaControl);
            Marshal.ReleaseComObject(pMediaEvent);

            pGraphBuilder = null;
            pMediaControl = null;
            pMediaEvent = null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }
    }
}
